package com.hengpeng.itfin.tools;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hengpeng.common.util.SignatureHelper;

public class DBUtil {
	private static final Log logger = LogFactory.getLog(DBUtil.class);

	public static Connection getConnection() {
		try {
			// mysql-----com.mysql.jdbc.Driver
			// sqlserver-----com.microsoft.sqlserver.jdbc.SQLServerDriver
			String driverName = "com.mysql.jdbc.Driver"; // 加载JDBC驱动
			String dbURL = "jdbc:mysql://localhost:3306/178cpw?useUnicode=true&characterEncoding=utf-8&user=root&password=123456"; // 连接服务器和数据库test
			String userName = "root"; // 用户名
			String userPwd = "123456"; // 密码
			Class.forName(driverName);
			Connection dbConn = DriverManager.getConnection(dbURL, userName,
					userPwd);
			return dbConn;
		} catch (Exception e) {
			logger.error("连接数据库服务器失败", e);
			e.printStackTrace();
			return null;
		}
	}

	public static ResultSet getList(String sql) {
		try {
			Connection conn = DBUtil.getConnection();
			Statement stmt = conn.createStatement();
			ResultSet rs = null;

			rs = stmt.executeQuery(sql);
			return rs;
		} catch (SQLException ex) {
			System.err.println(ex.getMessage());
		}

		return null;
	}

	public static boolean execute(String sql) {
		try {
			Connection conn = DBUtil.getConnection();

			Statement st = conn.createStatement();

			boolean result = st.execute(sql);

			st.close();

			conn.close();

			return result;

		} catch (Exception e) {
			logger.error("sql server执行sql语句失败，sql语句为:" + sql, e);
			return false;
		}
	}

	public static void main(String args[]) {
		 System.out.println("e10adc3949ba59abbe56e057f20f883e".equals(SignatureHelper.generateMD5Signture("123456", "utf-8")));
		
		
	}
}
